package com.eascs.web.o2o.dc.common.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.jdbc.core.JdbcTemplate;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.ResultSets;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.StoreProcedure;
import com.eascs.app.dao.TableData;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.web.o2o.report.constants.FefundConstants;
/**
 * 业务系统service--数据源
 * @author ycz
 *
 */
public abstract class BaseServiceDao {
	@Inject
	protected JdbcSession jdbcSessionForService;
	@Inject
	protected JdbcSession  jdbcSessionForSlaveService;
	public <T> T getByIdWithEntity(T entity) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.getByIdWithEntity(entity);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.getByIdWithEntity(entity);
	}

	public <T> int deleteByIdWithEntity(T entity) {
		return jdbcSessionForService.deleteByIdWithEntity(entity);
	}

	public <T> T getById(Class<T> entityClass, Object... id) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.getById(entityClass, id);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.getById(entityClass, id);
	}

	public <T> int deleteById(Class<T> entityClass, Object... id) {
		return jdbcSessionForService.deleteById(entityClass, id);
	}

	public <T> int insert(T entity) {
		return jdbcSessionForService.insert(entity);
	}

	public <T> int updateById(T entity) {
		return jdbcSessionForService.updateById(entity);
	}

	public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntitys(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findEntitys(sql, requiredType, params);
	}

	public <T> T findEntity(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntity(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findEntity(sql, requiredType, params);
	}

	public <T> List<T> findSimpleObjectList(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntitys(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findSimpleObjectList(sql, requiredType, params);
	}

	public <T> T findSimpleObject(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findSimpleObject(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findSimpleObject(sql, requiredType, params);
	}

	public Integer findInteger(String sql, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findInteger(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findInteger(sql, params);
	}

	public String findString(String sql, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findString(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findString(sql, params);
	}

	public TableData findTableData(String sql, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findTableData(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findTableData(sql, params);
	}

	public List<Object[]> findObjectsList(String sql, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findObjectsList(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findObjectsList(sql, params);
	}

	public int execute(String sql, Object... params) {
		return jdbcSessionForService.execute(sql, params);
	}

	public int[] batchExecute(String sql, List<Object[]> params) {
		return jdbcSessionForService.batchExecute(sql, params);
	}

	public ResultSets callSP(StoreProcedure storeProcedure) {
		return jdbcSessionForService.callSP(storeProcedure);
	}

	public String insertWithReturnKey(String insertSql, Object... params) {
		return jdbcSessionForService.insertWithReturnKey(insertSql, params);
	}

	public String getSql(String sql) {
		return jdbcSessionForService.getSql(sql);
	}

	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findDataWithPage(sqlPageQuery);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findDataWithPage(sqlPageQuery);
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcSessionForService.getJdbcTemplate();
	}
}
